package io.gitee.jingtuo.android.device.ui.main

import android.content.Context
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentWidth
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.Search
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.constraintlayout.compose.ConstraintLayout
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.preferencesDataStore
import androidx.lifecycle.viewmodel.compose.viewModel
import io.gitee.jingtuo.android.device.R
import io.gitee.jingtuo.android.device.ui.widget.PropView

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun MainPage(viewModel: MainViewModel = viewModel(), searchProp: () -> Unit) {
    val curScrollBehavior = TopAppBarDefaults.pinnedScrollBehavior()
    val deviceInfo = viewModel.deviceInfo().collectAsState()
    val uploadState = viewModel.uploadState().collectAsState()
    val context = LocalContext.current
    viewModel.loadDeviceInfo()
    Scaffold(
        topBar = {
            TopAppBar(
                title = {
                    Text(
                        text = "设备信息",
                        maxLines = 1,
                        overflow = TextOverflow.Ellipsis
                    )

                },
                actions =  {
                     IconButton(onClick = { searchProp() }) {
                        Icon(imageVector = Icons.Rounded.Search,
                            contentDescription = stringResource(R.string.search_pro)
                        )
                     }
                    IconButton(onClick = { viewModel.uploadDeviceData(context) }) {
                        Icon(painter = painterResource(R.drawable.outline_cloud_upload_24),
                            contentDescription = stringResource(R.string.upload_device_data)
                        )
                    }
                },
                scrollBehavior = curScrollBehavior
            )
        },
        content = { innerPadding ->
            DeviceInfoList(innerPadding, list = deviceInfo.value)
        }
    )
    if ("uploading" == uploadState.value.first) {
        //正在上传
    } else if ("success" == uploadState.value.first) {
        //上传成功
        AlertDialog(
            onDismissRequest = { /*TODO*/ },
            confirmButton = {
                TextButton(onClick = { viewModel.resetUploadState() }) {
                    Text(text = "我知道了")
                }
            },
            title = {
                Text(text = "温馨提示")
            },
            text = {
                Text(text = "上传成功")
            }
        )
    } else if ("failure" == uploadState.value.first) {
        AlertDialog(
            onDismissRequest = { /*TODO*/ },
            confirmButton = {
                TextButton(onClick = { viewModel.resetUploadState() }) {
                    Text(text = "我知道了")
                }
            },
            title = {
                Text(text = "温馨提示")
            },
            text = {
                Text(text = uploadState.value.second)
            }
        )
    }
}

@Composable
fun DeviceInfoList(innerPadding: PaddingValues, list: List<DeviceInfoItem>) {
    LazyColumn(modifier = Modifier.padding(innerPadding)) {
        items(list) { item ->
            ConstraintLayout(
                modifier = Modifier.fillMaxWidth()
            ) {
                val (nameId, pairId, searchId) = createRefs()
                Text(
                    text = item.name,
                    modifier = Modifier
                        .wrapContentWidth()
                        .constrainAs(nameId) {
                            top.linkTo(parent.top, 10.dp)
                            start.linkTo(parent.start, 16.dp)
                        }
                )
                PropView(item.pair, modifier = Modifier
                    .fillMaxWidth()
                    .constrainAs(pairId) {
                        start.linkTo(parent.start)
                        top.linkTo(nameId.bottom)
                    })
            }
        }
    }
}